// 用户注册
function register(temp) {
    var inputs = temp.getElementsByTagName('input');
    var username = inputs[0].value.trim();
    var password1 = inputs[1].value.trim();
    var password2 = inputs[2].value.trim();
    var email = inputs[3].value.trim();
    var nickname = inputs[4].value.trim();
    var description = inputs[5].value.trim();
    var sex = (inputs[6].checked) ? 'm' : 'f';
    var parameter = "user=" + username + "&pw1="+ password1 + "&pw2=" + password2 + "&email=" + 
        email + "&nickname=" + nickname + "&desc=" + description + "&sex=" + sex ;

    // 检测输入的参数
    function check_input () {
        var err = $('err_text');
        if (! username ) {
            err.innerHTML = '用户名不能为空';
            return 0;
        }

        if (!password1 || !password2) {
            err.innerHTML = '密码不能为空';
            return 0;
        }

        if (password1 != password2 ) {
            err.innerHTML = '两次密码不一致';
            return 0;
        }

        if (password1.length < 0) {
            err.innerHTML = '密码的长度要大于等于4';
            return 0;
        }    // end check_input function
        return 1;
    }   
    if (check_input()) {
        var url = './script/register.php?' + parameter;
        xhr_get(register_after, url);
    }
    return false;
}

function register_after (text) {
    var json = toJSON (text); 
    if (json.status == 0) 
        $('err_text').innerHTML = get_time() + json.text;
    else {
        $('err_text').innerHTML = get_time() + json.text;
    } 
}

// 用户登录
function login(elem) {
    var inputs = elem.getElementsByTagName ('input');
    var user = inputs[0].value.trim();
    var pawd = inputs[1].value.trim();
    function check () {
        var span = elem.getElementsByTagName('span')[0];
        if (! user) {
            span.innerHTML = '用户名不能为空';
            return 0;
        } 
        if (! pawd) {
            span.innerHTML = '密码不能为空';
            return 0;
        }
        return 1;
    }  // end check function
    if (check ()) {
        var url  = './script/login.php?username=' + user + '&password=' + pawd;
        xhr_get (login_after, url);
    }
    return false; 
}

function login_after (text) {
    var json = toJSON (text);
    var span = $('login_text');
    if (! json.status)
        span.innerHTML = json.text;
    else {                            //登录成功
        span.innerHTML = json.text;
        $('login_panel').style.display = "none";
        url = './script/login_ok.php';
        global_is_login = true;
        xhr_get (login_after_fetch_data, url);
    }
}

// 用户登陆后，从数据库中获取数据， 包括自己的信息，好友信息，群信息，最近联系等，
function login_after_fetch_data(text) {
    var json = toJSON (text);
    function update_user_info (user) {
        var user_item = $('chat_panel_head');
        user_item.getElementsByTagName('p')[0].innerHTML = user.nickname + ' ('+ user.name + ')';
        user_item.getElementsByTagName('input')[0].value = user.signed ? user.signed : '';
    }

    function update_friends (friends) {
        var template = '<div class="left"><img src="./images/si.png" ></div><div class="left"><p class="p1">{0} ({1})</p><p class="p2">{2}</p></div>';
        var chat_body = $('chat_panel_body_f');
        for (var i=0; i < friends.length; i++) {
            var li = c('li');
            li.innerHTML=String.format(template, friends[i].nickname, friends[i].name, friends[i].signed);
            chat_body.appendChild ( li );
        }
    }

    function update_zones (zones) {
        var template = '<div class="left"><img src="./images/si.png" ></div><div class="left"><p class="p1">{0} ({1})</p><p class="p2">{2}</p></div>';
        var chat_body = $('chat_panel_body_z');
        for (var i=0; i < friends.length; i++) {
            var li = c('li');
            li.innerHTML=String.format(template, friends[i].nickname, friends[i].name, friends[i].signed);
            chat_body.appendChild (li);
        }
    }

    update_user_info (json.info.user);
    update_friends (json.info.friends);
}

function logout () {
    var url = './script/logout.php';
    xhr_get (function () {}, url);
    global_is_login = false;
}

function logout_exit () {
    logout ();
    location.reload ('.');
}
